﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using FwProject.Core.TypeNumber;
using FwProject.Distribution.Dto;
using FwProject.TypeNumber.Dto;

namespace FwProject.Distribution
{
    /// <summary>
    /// app 司机管理装车接口
    /// </summary>
    public class DriverTypeNumberAppService: FwProjectAppServiceBase, IDriverTypeNumberAppService
    {
        public readonly IRepository<DriverTypeNumber, Guid> _repositoryDriverTypeNumber;
        public readonly IRepository<TypeNumberInfo, Guid> _repositoryTypeNumberInfo;
        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }
        public DriverTypeNumberAppService(IRepository<DriverTypeNumber, Guid> repositoryDriverTypeNumber,
            IRepository<TypeNumberInfo, Guid> repositoryTypeNumberInfo)
        {
            _repositoryDriverTypeNumber = repositoryDriverTypeNumber;
            _repositoryTypeNumberInfo = repositoryTypeNumberInfo;
            _guid = SequentialGuidGenerator.Instance;
        }

        /// <summary>
        /// App返回当前司机默认装车型号
        /// </summary>
        /// <returns></returns>
        public async Task<List<TypeNumberInfoDto>> GetDefaultDriverByTypeNumber() {
            var user = await UserManager.GetUserByIdAsync(AbpSession.UserId.Value);
            return  _repositoryDriverTypeNumber.GetAll().Where(o => o.DriverId == AbpSession.UserId.Value && o.FactoryinfoId==user.FactoryinfoId).Join(_repositoryTypeNumberInfo.GetAll().Where(o=>o.IsActive==true), i => i.TypeNumberId, n => n.Id, (i, n) => _map.Map<TypeNumberInfoDto>(n)).ToList();

        }
        /// <summary>
        /// 获取系统默认装车型号默认司机选择型号选中状态
        /// </summary>
        /// <returns></returns>
        public async Task<List<TypeNumberInfoDto>> GetSystemByTypeNumber()
        {
            var user = await UserManager.GetUserByIdAsync(AbpSession.UserId.Value);
            var List= _map.Map<List<TypeNumberInfoDto>>(_repositoryTypeNumberInfo.GetAll().Where(o => o.IsActive == true && o.FactoryinfoId == user.FactoryinfoId).OrderByDescending(m => m.CreationTime));
            var listId= _repositoryDriverTypeNumber.GetAll().Where(o => o.DriverId == AbpSession.UserId.Value && o.FactoryinfoId == user.FactoryinfoId).Join(_repositoryTypeNumberInfo.GetAll(), i => i.TypeNumberId, n => n.Id, (i, n) =>i.TypeNumberId).ToList();

           List.ForEach((item) =>
            {
                if (listId.Contains(item.Id))
                    item.IsDevierSelect = true;
            });
            return List;

        }
        /// <summary>
        /// App添加司机默认装车型号信息
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public async Task Edit(List<DriverTypeNumberInput> guids)
        {
            ///当前司机
            var user = await UserManager.GetUserByIdAsync(AbpSession.UserId.Value);
            if (guids.Count > 0)
            {
                var typeOldIds = guids.Select(o => o.TypeNumberId);
                var model = _repositoryDriverTypeNumber.GetAll().Where(o => o.DriverId == AbpSession.UserId.Value && o.FactoryinfoId == user.FactoryinfoId);
                if (model.Count() > 0)
                {
                    //获取差异化的数据
                    var removes = model.Where(o => typeOldIds.Any(r => r == o.TypeNumberId) == false).ToList();
                    Expression<Func<DriverTypeNumber, bool>> fun = f => removes.Any(s => s.Id == f.Id);
                    if (removes?.Count > 0)//判断是否有修改
                        for (int i = 0; i < removes.Count; i++)//执行删除操作
                            await _repositoryDriverTypeNumber.DeleteAsync(removes[i]);


                    var typenewIds = typeOldIds.Where(r => model.Any(o => o.TypeNumberId == r) == false);

                    if (typenewIds.Count() != 0)
                    {
                        foreach (var a in typenewIds)
                        {
                            var m = _map.Map<DriverTypeNumber>(guids.Where(o=>o.TypeNumberId==a).FirstOrDefault());
                            m.Id = _guid.Create();
                            await _repositoryDriverTypeNumber.InsertAsync(m);
                        }
                    }
                }
                else
                {
                    foreach (var item in guids)
                    {
                        var m = _map.Map<DriverTypeNumber>(item);
                        m.Id = _guid.Create();
                        _repositoryDriverTypeNumber.Insert(m);
                    }

                }

            }
        }
    }
}
